﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using IMC100APIDLL;

namespace PrgRun
{
    public partial class Form1 : Form
    {
        private Int32 Estop;
        private Int32 Enable;
        private Int32 SysMode;
        private Int32 PrgSts;
        private Int32 InchMode;
        private Int32 SysCoord;
        private Int32 SysDsmode;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)  //机器人连接按钮
        {
            byte[] bytes = IPAddress.Parse(textBoxIP.Text).GetAddressBytes();
            Array.Reverse(bytes);
            UInt32 IpAddr = BitConverter.ToUInt32(bytes, 0);
            UInt16 IpPort = 2222;
            Int32 ret = 0;

            ret = IMC100API.IMC100_Init_ETH(IpAddr, IpPort, 5, 0);
            if (ret < 0)
            {
                MessageBox.Show("机器人连接失败");
                return;
            }
        
            
            ret = IMC100API.IMC100_AcqPermit(1, 0);  //强制获取控制权
            if (ret < 0)
            {
                MessageBox.Show("控制权获取失败");
                //return;
            }

            Int32 type = 2;
            String passwordStr = "000000";  //密码与示教器上一致
            Byte[] password = new Byte[8];
            password = System.Text.Encoding.Default.GetBytes(passwordStr);
            ret = IMC100API.IMC100_UserLogin(type, password, 0);  //登陆到管理模式(可依据实际函数需求选择登陆的模式)
            if (ret < 0)
            {
                MessageBox.Show("登陆失败");
                //return;
            }

            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)  //机器人断开按钮
        {
            Int32 ret = 0;
            ret = IMC100API.IMC100_Exit_ETH(0);
            if (ret < 0)
            {
                MessageBox.Show("断开连接失败");
            }

            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)  //计时器 500ms
        {
            Int32 ret = 0;
            ROBOT_POS pos = new ROBOT_POS();
            ret = IMC100API.IMC100_Get_PosHereJ(ref pos, 0);  //显示机器人位置
            if (ret >= 0)
            {
                textBoxPos.Text = String.Format("{0:0.000},{1:0.000},{2:0.000},{3:0.000},{4:0.000},{5:0.000}", pos.pos[0], pos.pos[1], pos.pos[2], pos.pos[3], pos.pos[4], pos.pos[5]);
            }

            Int32 err = 0;
            ret = IMC100API.IMC100_Get_SysErr(ref err, 0);  //显示机器人故障
            if (ret >= 0)
            {
                textBoxErr.Text = String.Format("{0:x4}", err);
            }

            Int32 sts = 0;
            ret = IMC100API.IMC100_Get_EStopSts(ref sts, 0);  //急停状态 Estop
            if (ret >= 0)
            {
                Estop = sts;
                if (Estop == 0)
                {
                    buttonEStop.Text = "急停松开";
                }
                else
                {
                    buttonEStop.Text = "急停按下";
                }
            }

            ret = IMC100API.IMC100_Get_MotorSts(ref sts, 0);  //使能状态 Enable
            if (ret >= 0)
            {
                Enable = sts;
                if (Enable == 0)
                {
                    buttonEnable.Text = "未使能";
                }
                else
                {
                    buttonEnable.Text = "使能";
                }
            }
            ret = IMC100API.IMC100_Get_DsMode(ref sts, 0);  //数据流模式 SysDsmode
            if (ret >= 0)
            {
                SysDsmode = sts;
                if (SysDsmode == 1)
                {
                    button3.Text = "数据流模式开启";
                }
                else
                {
                    button3.Text = "数据流模式关闭";
                }
            }
            Int32 mode = 0;
            ret = IMC100API.IMC100_Get_Mode(ref mode, 0);  //系统模式
            if (ret >= 0)
            {
                SysMode = mode;
                if (SysMode == 1)
                {
                    buttonMode.Text = "示教";
                }
                else if (SysMode == 2)
                {
                    buttonMode.Text = "再现";
                }
            }

            Byte[] prgPath = new Byte[128];
            ret = IMC100API.IMC100_Get_CurPrgPath(prgPath, 0);  //当前程序路径
            String path = System.Text.Encoding.Default.GetString(prgPath);
            if (ret >= 0)
            {
                textBoxPath.Text = path;
            }

            //ret = IMC100API.IMC100_Get_PrgSts(ref sts, 0);  //当前程序状态
            //if (ret >= 0)
            //{
            //    PrgSts = sts;
            //    if (PrgSts == 0)
            //    {
            //        textBoxPrg.Text = "0:停止";
            //    }
            //    else if (PrgSts == 1)
            //    {
            //        textBoxPrg.Text = "1:运行";
            //    }
            //    else if (PrgSts == 4)
            //    {
            //        textBoxPrg.Text = "4:暂停";
            //    }
            //}

            ret = IMC100API.IMC100_Get_InchMode(ref mode, 0);  //当前示教方式：连续or寸动
            if (ret >= 0)
            {
                InchMode = mode;
                if (InchMode == 0)
                {
                    buttonInchMode.Text = "连续示教";
                }
                else
                {
                    buttonInchMode.Text = "寸动示教";
                }
            }

            Int32 coord = 0;
            ret = IMC100API.IMC100_Get_Coord(ref coord, 0);
            if (ret >= 0)
            {
                SysCoord = coord;
                comboBoxCoord.SelectedIndex = SysCoord - 1;
            }
            for(int i=0;i <16;i++)
            {
                ret = IMC100API.IMC100_Get_DI(i, ref coord, 0);
                if (ret == 0)
                {
                    Label M = (Label)Controls[$"lbl_{i}"];
                    M.BackColor = (coord == 1 ?Color.Green:Color.Gray);
                   
                }
            }
            ret = IMC100API.IMC100_Get_Print(prgPath, 0);
            if (ret == 0)
            {
                txt_MSG.Text = System.Text.Encoding.Default.GetString(prgPath);                
            }
        }

        private void buttonEStop_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (Estop == 0)
            {
                cmd = 1;
            }
            else
            {
                cmd = 0;
            }

            ret = IMC100API.IMC100_EmergStop(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("急停操作失败");
            }
        }

        private void buttonEnable_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (Enable == 0)
            {
                cmd = 1;
            }
            else
            {
                cmd = 0;
            }

            ret = IMC100API.IMC100_MotorEnable(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("使能控制失败");
            }
        }

        private void buttonMode_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (SysMode == 1)
            {
                cmd = 2;
            }
            else
            {
                cmd = 1;
            }

            ret = IMC100API.IMC100_Set_Mode(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("模式切换失败");
            }
        }

        private void buttonErrClr_Click(object sender, EventArgs e)
        {
            Int32 ret = 0;
            ret = IMC100API.IMC100_ResetErr(0);
            if (ret < 0)
            {
                MessageBox.Show("复位控制失败");
            }
        }

        private void buttonPathSet_Click(object sender, EventArgs e)
        {
            Int32 ret = 0;
            String path = textBoxPathSet.Text;
            Byte[] prgPath = new Byte[128];
            prgPath = System.Text.Encoding.Default.GetBytes(path);
            ret = IMC100API.IMC100_Set_CurPrgPath(prgPath, 0);
            if (ret < 0)
            {
                MessageBox.Show("设置程序路径失败");
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (PrgSts != 1)
            {
                cmd = 1;
            }
            else
            {
                return;
            }

            ret = IMC100API.IMC100_PrgCtrl(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("启动失败");
            }
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
           
            //cmd = 4;
            ret = IMC100API.IMC100_PrgCtrl(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("暂停失败");
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (PrgSts != 0)
            {
                cmd = 0;
            }
            else
            {
                return;
            }

            ret = IMC100API.IMC100_PrgCtrl(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("停止失败");
            }
        }

        private void comboBoxCoord_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 coord = 0;
            Int32 ret = 0;
            coord = comboBoxCoord.SelectedIndex + 1;
            ret = IMC100API.IMC100_Set_Coord(coord, 0);
            if (ret < 0)
            {
                MessageBox.Show("设置坐标系失败");
            }
        }

        private void buttonInchMode_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (InchMode == 0)
            {
                cmd = 1;
            }
            else
            {
                cmd = 0;
            }

            ret = IMC100API.IMC100_InchMode(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("示教方式切换失败");
            }
        }

        private void buttonJ1P_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(1, 1);
        }

        private void buttonJ1P_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(1, 0);
        }

        private void buttonJ1N_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(1, -1);
        }

        private void buttonJ1N_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(1, 0);
        }

        private void buttonJ2P_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(2, 1);
        }

        private void buttonJ2P_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(2, 0);
        }

        private void buttonJ2N_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(2, -1);
        }

        private void buttonJ2N_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(2, 0);
        }

        private void buttonJ3P_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(3, 1);
        }

        private void buttonJ3P_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(3, 0);
        }

        private void buttonJ3N_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(3, -1);
        }

        private void buttonJ3N_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(3, 0);
        }

        private void buttonJ4P_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(4, 1);
        }

        private void buttonJ4P_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(4, 0);
        }

        private void buttonJ4N_ClickDown(object sender, MouseEventArgs e)
        {
            JogFunc(4, -1);
        }

        private void buttonJ4N_ClickUp(object sender, MouseEventArgs e)
        {
            JogFunc(4, 0);
        }

        private void JogFunc(Int32 axis, Int32 cmd)
        {
            Int32 coord = 0;
            Int32 ret = 0;
            if (SysCoord == 1)
            {
                coord = 0;
            }
            else
            {
                coord = 1;
            }

            ret = IMC100API.IMC100_Jog(coord, axis, cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("示教失败");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Int32 cmd = 0;
            Int32 ret = 0;
            if (SysDsmode == 1)
            {
                cmd = 0;
            }
            else
            {
                cmd = 1;
            }

            ret = IMC100API.IMC100_DsMode(cmd, 0);
            if (ret < 0)
            {
                MessageBox.Show("数据流模式控制失败");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int32 ret = 0;
            String strPos = textBoxP.Text;
            String strPosB = textBoxPB.Text;
            String[] posArr = strPos.Split(',');
            String[] posBArr = strPosB.Split(',');
            if (posArr.Length < 6)
            {
                MessageBox.Show("缺少目标点或格式错误");
                return;
            }
            if (posBArr.Length < 4)
            {
                MessageBox.Show("缺少目标点臂参数或格式错误");
                return;
            }
            ROBOT_POS pos = new ROBOT_POS();
            pos.pos = new Double[6];
            pos.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                pos.pos[i] = Double.Parse(posArr[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                pos.armType[i] = Int32.Parse(posBArr[i]);
            }
            pos.coord = 2;

            ret = IMC100API.IMC100_MovJ2(pos, 100, 0, 0);
            if (ret < 0)
            {
                MessageBox.Show("MovJ控制失败");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Int32 ret = 0;
            String strPos = textBoxP.Text;
            String strPosB = textBoxPB.Text;
            String[] posArr = strPos.Split(',');
            String[] posBArr = strPosB.Split(',');
            if (posArr.Length < 6)
            {
                MessageBox.Show("缺少目标点或格式错误");
                return;
            }
            if (posBArr.Length < 4)
            {
                MessageBox.Show("缺少目标点臂参数或格式错误");
                return;
            }
            ROBOT_POS pos = new ROBOT_POS();
            pos.pos = new Double[6];
            pos.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                pos.pos[i] = Double.Parse(posArr[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                pos.armType[i] = Int32.Parse(posBArr[i]);
            }
            pos.coord = 2;
            ret = IMC100API.IMC100_MovL2(pos, 1, 0, 0);
            if (ret < 0)
            {
                MessageBox.Show("Movl控制失败");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Int32 ret = 0;
            String strPosMid = textBoxP.Text;
            String strPosDst = textBoxP1.Text;
            String[] posArrMid = strPosMid.Split(',');
            String[] posArrDst = strPosDst.Split(',');
            String strPosBMid = textBoxPB.Text;
            String strPosBDst = textBoxPB1.Text;
            String[] posArrBMid = strPosBMid.Split(',');
            String[] posArrBDst = strPosBDst.Split(',');
            if (posArrMid.Length < 6)
            {
                MessageBox.Show("圆弧中间点缺少目标点或格式错误");
                return;
            }
            if (posArrDst.Length < 6)
            {
                MessageBox.Show("圆弧终点缺少目标点或格式错误");
                return;
            }
            if (posArrBMid.Length < 4)
            {
                MessageBox.Show("圆弧中间点臂参数缺少目标点或格式错误");
                return;
            }
            if (posArrBDst.Length < 4)
            {
                MessageBox.Show("圆弧终点臂参数缺少目标点或格式错误");
                return;
            }
            ROBOT_POS posMid = new ROBOT_POS();
            posMid.pos = new Double[6];
            posMid.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                posMid.pos[i] = Double.Parse(posArrMid[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                posMid.armType[i] = Int32.Parse(posArrBMid[i]);
            }
            posMid.coord = 2;
            ROBOT_POS posDst = new ROBOT_POS();
            posDst.pos = new Double[6];
            posDst.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                posDst.pos[i] = Double.Parse(posArrDst[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                posDst.armType[i] = Int32.Parse(posArrBDst[i]);
            }
            posDst.coord = 2;
            ret = IMC100API.IMC100_MovC2(posMid, posDst, 100, 0, 0);
            if (ret < 0)
            {
                MessageBox.Show("Movc控制失败");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //初始化定义
            Int32 ret = 0;
            //点位赋值
            String strPosStart = FlyPoint1.Text;
            String strPosEnd = FlyPoint2.Text;
            String[] posArrStart = strPosStart.Split(',');
            String[] posArrEnd = strPosEnd.Split(',');
            String strPosBStart = Flypointb1.Text;
            String strPosBEnd = Flypointb2.Text;
            String[] posArrBStart = strPosBStart.Split(',');
            String[] posArrBEnd = strPosBEnd.Split(',');
            String Vaule = FlyVaule.Text;
            Int32 TVaule = 50;
            //判断输入的点位是否正确
            if (posArrStart.Length < 6)
            {
                MessageBox.Show("缺少目标点或格式错误");
                return;
            }
            if (posArrBStart.Length < 4)
            {
                MessageBox.Show("缺少目标点臂参数或格式错误");
                return;
            }
            if (posArrEnd.Length < 6)
            {
                MessageBox.Show("缺少目标点或格式错误");
                return;
            }
            if (posArrBEnd.Length < 4)
            {
                MessageBox.Show("缺少目标点臂参数或格式错误");
                return;
            }
            if (posArrStart.Length < 6)
            {
                MessageBox.Show("缺少目标点或格式错误");
                return;
            }
            //点位赋值
            ROBOT_POS posStart = new ROBOT_POS();
            posStart.pos = new Double[6];
            posStart.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                posStart.pos[i] = Double.Parse(posArrStart[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                posStart.armType[i] = Int32.Parse(posArrBStart[i]);
            }
            posStart.coord = 2;

            ROBOT_POS PosEnd = new ROBOT_POS();
            PosEnd.pos = new Double[6];
            PosEnd.armType = new Int32[4];
            for (int i = 0; i < 6; i++)
            {
                PosEnd.pos[i] = Double.Parse(posArrEnd[i]);
            }
            for (int i = 0; i < 4; i++)
            {
                PosEnd.armType[i] = Int32.Parse(posArrBEnd[i]);
            }
            PosEnd.coord = 2;

            //定义锁存点
            ROBOT_POS POSFLY = new ROBOT_POS();
            //字符串转整型数值
            bool result = int.TryParse(Vaule, out TVaule);
            if (result == false)
            {
                TVaule = 50;
                MessageBox.Show("数值转换失败");
            }

            //运动IO结构体定义
            MOV_IO PosIO = new MOV_IO();
            //飞拍必须使用OUT[15]作为相机的硬件触发信号，同时该输出信号也是锁存位置的探针触发信号
            PosIO.ioNo = 15;
            PosIO.ioVa = 1;
            PosIO.kind = 1;
            PosIO.value =TVaule;
            //开启锁存功能
            ret = IMC100API.IMC100_LatchEnable(1, 0);
            //清除锁存过的位置
            ret = IMC100API.IMC100_Clr_LatchPos(0);

            //运动到飞拍起始位置
            ret = IMC100API.IMC100_MovJ2(posStart, 50, 0, 0);
            if (ret < 0)
            {
                MessageBox.Show("MovJ控制失败");
            }
            //运作至飞拍终点位置，并在路径中触发IO，使相机拍照并锁存位置
            ret = IMC100API.IMC100_MovL2_IO(PosEnd, 50, 0, PosIO, 1,0);
            if (ret < 0)
            {
                MessageBox.Show("MovJ控制失败");
            }

            //此处传递出来的是double型的数值，是由于此函数传出来的是关节值，也就是在关节坐标系下的坐标值，是Pos结构体里的double pos[6];
            //此函数通过ref Sts来查看是否有锁存位置值，建议在此处做循环，以免在未完成运动而导致未查询到锁存值
            Int32 Sts =0;
            while (Sts != 1)
            {
                ret = IMC100API.IMC100_Get_LatchPos(0, ref Sts, POSFLY.pos, 0);
            }
            //由于传递出来的值为关节值,将点位的坐标系设为关节坐标系
            POSFLY.coord = 1;

            //通过函数将关节坐标系下的点转换为基座标系下的点
            ROBOT_POS poscnvrt = new ROBOT_POS();
            poscnvrt.coord = 2;
            ret = IMC100API.IMC100_Get_PosCnvt(ref POSFLY, ref poscnvrt, 0);
            if (ret < 0)
            {
                MessageBox.Show("转换失败");
            }

            //清除锁存过的位置
            ret = IMC100API.IMC100_Clr_LatchPos(0);
            //关闭锁存功能
            ret = IMC100API.IMC100_LatchEnable(0, 0);


            //显示锁存位置
            FlyPoint3.Text = String.Format("{0:0.000},{1:0.000},{2:0.000},{3:0.000},{4:0.000},{5:0.000}", poscnvrt.pos[0], poscnvrt.pos[1], poscnvrt.pos[2], poscnvrt.pos[3], poscnvrt.pos[4], poscnvrt.pos[5]);
            FlyponitB3.Text = String.Format("{0:0},{1:0},{2:0},{3:0}", poscnvrt.armType[0], poscnvrt.armType[1], poscnvrt.armType[2], poscnvrt.armType[3]);


        }

        private void btn_out0_Click(object sender, EventArgs e)
        {
            Button M= (Button)sender;
            int DO = Convert.ToInt16(M.Text.Replace("OUT", ""));
            if (M.Tag.ToString() == "0")
            {
                Int32 ret = IMC100API.IMC100_Set_DO(DO, 1, 0);
                if (ret != 0)
                {
                    MessageBox.Show("设置失败");
                    return;
                }
                M.Tag = "1";
                M.BackColor = Color.Green;
            }
            else
            {
                Int32 ret = IMC100API.IMC100_Set_DO(DO, 0, 0);
                if (ret != 0)
                {
                    MessageBox.Show("设置失败");
                    return;
                }
                M.Tag = "0";
                M.BackColor = Color.Gray;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strLbl = "label";
            for (int i = 0; i < 16; i++)
            {
                strLbl = string.Format($"lbl_{i}");
                Label M = (Label)Controls[strLbl];
                M.Text = $"IN[{i}]";
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
          
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_coilAddress.Text==""?"0":txt_coilAddress.Text);
            int Num = 0;
            Num = Convert.ToInt32((txt_num.Text=="" ? "1" : txt_num.Text));
            int OutValue = 0;
            int ret = IMC100API.IMC100_Get_ModbusCoil(value, Num, ref OutValue,0);
            if (ret < 0)
            {
                MessageBox.Show("查询线圈失败");
                return;
            }
            txt_coilValue.Text = OutValue.ToString();


        }

        private void btn_SetCoil_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_coilAddress.Text == "" ? "0" : txt_coilAddress.Text);
            int Num = 0;
            Num = Convert.ToInt32((txt_num.Text == "" ? "1" : txt_num.Text));
            int OutValue =txt_coilValue.Text=="0"?0:1;
            int ret = IMC100API.IMC100_Set_ModbusCoil(value, Num,  OutValue, 0);
            if (ret < 0)
            {
                MessageBox.Show("设置线圈失败");
                return;
            }
            txt_coilValue.Text = OutValue.ToString();

        }

        private void btn_searchV_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_Vaddress.Text == "" ? "0" : txt_Vaddress.Text);
            int Num = 0;
            Num = Convert.ToInt32((txt_Vnum.Text == "" ? "1" : txt_Vnum.Text));
            float[] OutValue=new float[Num] ;
            int ret = IMC100API.IMC100_Get_ModbusRegFloat(value, Num,  OutValue, 0);
            if (ret < 0)
            {
                MessageBox.Show("查询浮点失败");
                return;
            }
            string output = "";
            for(int i =0; i< OutValue.Count();i++)
            {
                output += OutValue[i];
            }
            txt_VValue.Text = output;

        }

        private void btn_SetVvalue_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_Vaddress.Text == "" ? "0" : txt_Vaddress.Text);
            int Num = 0;
            Num = Convert.ToInt32((txt_Vnum.Text == "" ? "1" : txt_Vnum.Text));
            float[] OutValue = new float[Num] ;
            
            for(int i =0; i< Num;i++)
            {
               
                    string strCout = "";
                    try
                    {
                         strCout = txt_VValue.Text.ElementAt(i).ToString();
                    }
                    catch
                    {
                        strCout = "0";
                    }
                OutValue[i] = (float)Convert.ToDouble(strCout);
            }
            
            int ret = IMC100API.IMC100_Set_ModbusRegFloat(value, Num, OutValue, 0);
            if (ret < 0)
            {
                MessageBox.Show("设置浮点失败");
                return;
            }
            

        }

        private void btn_putSpeed_Click(object sender, EventArgs e)
        {
            int ispeed = Convert.ToInt32(txt_PutSpeed.Text);
            int ret = IMC100API.IMC100_Set_B(10, ispeed,  0);//b10
            if (ret < 0)
            {
                MessageBox.Show("设置速度失败");
                return;
            }
        }

        private void btn_kongpao_Click(object sender, EventArgs e)
        {          
            int ret = IMC100API.IMC100_Set_B(0, 0, 0);//b0==0
            if (ret < 0)
            {
                MessageBox.Show("设置生产模式失败");
                return;
            }
        }

        private void btn_Product_Click(object sender, EventArgs e)
        {
            int ret = IMC100API.IMC100_Set_B(0, 1, 0);//b0==0
            if (ret < 0)
            {
                MessageBox.Show("设置生产模式失败");
                return;
            }
        }

    

        private void trackBar1_Scroll_1(object sender, EventArgs e)
        {
            int ret = IMC100API.IMC100_Set_Vel(Convert.ToInt32(trackBar1.Value), 0);
            if (ret < 0)
            {
                MessageBox.Show("设置速度失败");
            }
        }

        private void btn_Bsearch_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_Baddress.Text == "" ? "0" : txt_Baddress.Text);         
            int OutValue=0;
            int ret = IMC100API.IMC100_Get_B(value, ref OutValue, 0);
            if (ret < 0)
            {
                MessageBox.Show("查询B失败");
                return;
            }
            txt_Bvalue.Text = OutValue.ToString();
        }

        private void btn_Bset_Click(object sender, EventArgs e)
        {
            int value = Convert.ToInt32(txt_Baddress.Text == "" ? "0" : txt_Baddress.Text);
            int OutValue = Convert.ToInt32(txt_Bvalue.Text == "" ? "0" : txt_Bvalue.Text);            
            int ret = IMC100API.IMC100_Set_B(value,  OutValue, 0);
            if (ret < 0)
            {
                MessageBox.Show("查询B失败");
                return;
            }
        }
    }
}
